package cn.edu.scau.cmi.hombio.web.rest;

import cn.edu.scau.cmi.hombio.dao.MedicineDAO;
import cn.edu.scau.cmi.hombio.dao.MedicinePriceDAO;
import cn.edu.scau.cmi.hombio.dao.MedicineSupplierDAO;
import cn.edu.scau.cmi.hombio.dao.SupplierDAO;

import cn.edu.scau.cmi.hombio.domain.Medicine;
import cn.edu.scau.cmi.hombio.domain.MedicinePrice;
import cn.edu.scau.cmi.hombio.domain.MedicineSupplier;
import cn.edu.scau.cmi.hombio.domain.Supplier;

import cn.edu.scau.cmi.hombio.service.MedicineSupplierService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Spring Rest controller that handles CRUD requests for MedicineSupplier entities
 * 
 */

@Controller("MedicineSupplierRestController")

public class MedicineSupplierRestController {

	/**
	 * DAO injected by Spring that manages Medicine entities
	 * 
	 */
	@Autowired
	private MedicineDAO medicineDAO;

	/**
	 * DAO injected by Spring that manages MedicinePrice entities
	 * 
	 */
	@Autowired
	private MedicinePriceDAO medicinePriceDAO;

	/**
	 * DAO injected by Spring that manages MedicineSupplier entities
	 * 
	 */
	@Autowired
	private MedicineSupplierDAO medicineSupplierDAO;

	/**
	 * DAO injected by Spring that manages Supplier entities
	 * 
	 */
	@Autowired
	private SupplierDAO supplierDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for MedicineSupplier entities
	 * 
	 */
	@Autowired
	private MedicineSupplierService medicineSupplierService;

	/**
	 * Select an existing MedicineSupplier entity
	 * 
	 */
	@RequestMapping(value = "/MedicineSupplier/{medicinesupplier_id}", method = RequestMethod.GET)
	@ResponseBody
	public MedicineSupplier loadMedicineSupplier(@PathVariable Integer medicinesupplier_id) {
		return medicineSupplierDAO.findMedicineSupplierByPrimaryKey(medicinesupplier_id);
	}

	/**
	* Create a new MedicinePrice entity
	* 
	*/
	@RequestMapping(value = "/MedicineSupplier/{medicinesupplier_id}/medicinePrices", method = RequestMethod.POST)
	@ResponseBody
	public MedicinePrice newMedicineSupplierMedicinePrices(@PathVariable Integer medicinesupplier_id, @RequestBody MedicinePrice medicineprice) {
		medicineSupplierService.saveMedicineSupplierMedicinePrices(medicinesupplier_id, medicineprice);
		return medicinePriceDAO.findMedicinePriceByPrimaryKey(medicineprice.getId());
	}

	/**
	* Create a new Supplier entity
	* 
	*/
	@RequestMapping(value = "/MedicineSupplier/{medicinesupplier_id}/supplier", method = RequestMethod.POST)
	@ResponseBody
	public Supplier newMedicineSupplierSupplier(@PathVariable Integer medicinesupplier_id, @RequestBody Supplier supplier) {
		medicineSupplierService.saveMedicineSupplierSupplier(medicinesupplier_id, supplier);
		return supplierDAO.findSupplierByPrimaryKey(supplier.getId());
	}

	/**
	* Save an existing Medicine entity
	* 
	*/
	@RequestMapping(value = "/MedicineSupplier/{medicinesupplier_id}/medicine", method = RequestMethod.PUT)
	@ResponseBody
	public Medicine saveMedicineSupplierMedicine(@PathVariable Integer medicinesupplier_id, @RequestBody Medicine medicine) {
		medicineSupplierService.saveMedicineSupplierMedicine(medicinesupplier_id, medicine);
		return medicineDAO.findMedicineByPrimaryKey(medicine.getId());
	}

	/**
	* View an existing MedicinePrice entity
	* 
	*/
	@RequestMapping(value = "/MedicineSupplier/{medicinesupplier_id}/medicinePrices/{medicineprice_id}", method = RequestMethod.GET)
	@ResponseBody
	public MedicinePrice loadMedicineSupplierMedicinePrices(@PathVariable Integer medicinesupplier_id, @PathVariable Integer related_medicineprices_id) {
		MedicinePrice medicineprice = medicinePriceDAO.findMedicinePriceByPrimaryKey(related_medicineprices_id, -1, -1);

		return medicineprice;
	}

	/**
	* Delete an existing Medicine entity
	* 
	*/
	@RequestMapping(value = "/MedicineSupplier/{medicinesupplier_id}/medicine/{medicine_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteMedicineSupplierMedicine(@PathVariable Integer medicinesupplier_id, @PathVariable Integer related_medicine_id) {
		medicineSupplierService.deleteMedicineSupplierMedicine(medicinesupplier_id, related_medicine_id);
	}

	/**
	* Get Medicine entity by MedicineSupplier
	* 
	*/
	@RequestMapping(value = "/MedicineSupplier/{medicinesupplier_id}/medicine", method = RequestMethod.GET)
	@ResponseBody
	public Medicine getMedicineSupplierMedicine(@PathVariable Integer medicinesupplier_id) {
		return medicineSupplierDAO.findMedicineSupplierByPrimaryKey(medicinesupplier_id).getMedicine();
	}

	/**
	* View an existing Supplier entity
	* 
	*/
	@RequestMapping(value = "/MedicineSupplier/{medicinesupplier_id}/supplier/{supplier_id}", method = RequestMethod.GET)
	@ResponseBody
	public Supplier loadMedicineSupplierSupplier(@PathVariable Integer medicinesupplier_id, @PathVariable Integer related_supplier_id) {
		Supplier supplier = supplierDAO.findSupplierByPrimaryKey(related_supplier_id, -1, -1);

		return supplier;
	}

	/**
	* Save an existing Supplier entity
	* 
	*/
	@RequestMapping(value = "/MedicineSupplier/{medicinesupplier_id}/supplier", method = RequestMethod.PUT)
	@ResponseBody
	public Supplier saveMedicineSupplierSupplier(@PathVariable Integer medicinesupplier_id, @RequestBody Supplier supplier) {
		medicineSupplierService.saveMedicineSupplierSupplier(medicinesupplier_id, supplier);
		return supplierDAO.findSupplierByPrimaryKey(supplier.getId());
	}

	/**
	* Delete an existing Supplier entity
	* 
	*/
	@RequestMapping(value = "/MedicineSupplier/{medicinesupplier_id}/supplier/{supplier_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteMedicineSupplierSupplier(@PathVariable Integer medicinesupplier_id, @PathVariable Integer related_supplier_id) {
		medicineSupplierService.deleteMedicineSupplierSupplier(medicinesupplier_id, related_supplier_id);
	}

	/**
	* Create a new MedicineSupplier entity
	* 
	*/
	@RequestMapping(value = "/MedicineSupplier", method = RequestMethod.POST)
	@ResponseBody
	public MedicineSupplier newMedicineSupplier(@RequestBody MedicineSupplier medicinesupplier) {
		medicineSupplierService.saveMedicineSupplier(medicinesupplier);
		return medicineSupplierDAO.findMedicineSupplierByPrimaryKey(medicinesupplier.getId());
	}

	/**
	* View an existing Medicine entity
	* 
	*/
	@RequestMapping(value = "/MedicineSupplier/{medicinesupplier_id}/medicine/{medicine_id}", method = RequestMethod.GET)
	@ResponseBody
	public Medicine loadMedicineSupplierMedicine(@PathVariable Integer medicinesupplier_id, @PathVariable Integer related_medicine_id) {
		Medicine medicine = medicineDAO.findMedicineByPrimaryKey(related_medicine_id, -1, -1);

		return medicine;
	}

	/**
	* Delete an existing MedicinePrice entity
	* 
	*/
	@RequestMapping(value = "/MedicineSupplier/{medicinesupplier_id}/medicinePrices/{medicineprice_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteMedicineSupplierMedicinePrices(@PathVariable Integer medicinesupplier_id, @PathVariable Integer related_medicineprices_id) {
		medicineSupplierService.deleteMedicineSupplierMedicinePrices(medicinesupplier_id, related_medicineprices_id);
	}

	/**
	* Show all MedicineSupplier entities
	* 
	*/
	@RequestMapping(value = "/MedicineSupplier", method = RequestMethod.GET)
	@ResponseBody
	public List<MedicineSupplier> listMedicineSuppliers() {
		return new java.util.ArrayList<MedicineSupplier>(medicineSupplierService.loadMedicineSuppliers());
	}

	/**
	* Save an existing MedicineSupplier entity
	* 
	*/
	@RequestMapping(value = "/MedicineSupplier", method = RequestMethod.PUT)
	@ResponseBody
	public MedicineSupplier saveMedicineSupplier(@RequestBody MedicineSupplier medicinesupplier) {
		medicineSupplierService.saveMedicineSupplier(medicinesupplier);
		return medicineSupplierDAO.findMedicineSupplierByPrimaryKey(medicinesupplier.getId());
	}

	/**
	* Save an existing MedicinePrice entity
	* 
	*/
	@RequestMapping(value = "/MedicineSupplier/{medicinesupplier_id}/medicinePrices", method = RequestMethod.PUT)
	@ResponseBody
	public MedicinePrice saveMedicineSupplierMedicinePrices(@PathVariable Integer medicinesupplier_id, @RequestBody MedicinePrice medicineprices) {
		medicineSupplierService.saveMedicineSupplierMedicinePrices(medicinesupplier_id, medicineprices);
		return medicinePriceDAO.findMedicinePriceByPrimaryKey(medicineprices.getId());
	}

	/**
	* Get Supplier entity by MedicineSupplier
	* 
	*/
	@RequestMapping(value = "/MedicineSupplier/{medicinesupplier_id}/supplier", method = RequestMethod.GET)
	@ResponseBody
	public Supplier getMedicineSupplierSupplier(@PathVariable Integer medicinesupplier_id) {
		return medicineSupplierDAO.findMedicineSupplierByPrimaryKey(medicinesupplier_id).getSupplier();
	}

	/**
	* Delete an existing MedicineSupplier entity
	* 
	*/
	@RequestMapping(value = "/MedicineSupplier/{medicinesupplier_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteMedicineSupplier(@PathVariable Integer medicinesupplier_id) {
		MedicineSupplier medicinesupplier = medicineSupplierDAO.findMedicineSupplierByPrimaryKey(medicinesupplier_id);
		medicineSupplierService.deleteMedicineSupplier(medicinesupplier);
	}

	/**
	* Create a new Medicine entity
	* 
	*/
	@RequestMapping(value = "/MedicineSupplier/{medicinesupplier_id}/medicine", method = RequestMethod.POST)
	@ResponseBody
	public Medicine newMedicineSupplierMedicine(@PathVariable Integer medicinesupplier_id, @RequestBody Medicine medicine) {
		medicineSupplierService.saveMedicineSupplierMedicine(medicinesupplier_id, medicine);
		return medicineDAO.findMedicineByPrimaryKey(medicine.getId());
	}

	/**
	* Show all MedicinePrice entities by MedicineSupplier
	* 
	*/
	@RequestMapping(value = "/MedicineSupplier/{medicinesupplier_id}/medicinePrices", method = RequestMethod.GET)
	@ResponseBody
	public List<MedicinePrice> getMedicineSupplierMedicinePrices(@PathVariable Integer medicinesupplier_id) {
		return new java.util.ArrayList<MedicinePrice>(medicineSupplierDAO.findMedicineSupplierByPrimaryKey(medicinesupplier_id).getMedicinePrices());
	}

	/**
	* Register custom, context-specific property editors
	* 
	*/
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}
}